//
//  PhoneticReadingPlayBtn.m
//  ebook
//
//  Created by mt on 16/10/26.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "PhoneticReadingPlayBtn.h"
#import "ReaderToolBar.h"
#import "PhoneticReadingToolBar.h"
#import "PhoneticReadingManager.h"

@implementation PhoneticReadingPlayBtn

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)btnClick:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(phoneticReadingPlayBtnClick:)]) {
        [self.delegate phoneticReadingPlayBtnClick:self];
    }
}

- (void)showOnView:(UIView *)view {
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.6f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.layer addAnimation:transition forKey:nil];
    
    self.hidden = NO;
    
    BOOL isPlaying = [PhoneticReadingManager shareManager].isPlaying;
    BOOL isNight = [ReaderGlobalModel shareModel].readerConfig.IsNight;
    UIImage *image;
    if (!isNight) {
        if (!isPlaying) {
            image = [UIImage imageNamed:@"BookContent_bofang_nl"];
        } else {
            image = [UIImage imageNamed:@"BookContent_bofang_sl"];
        }
    } else {
        if (!isPlaying) {
            image = [UIImage imageNamed:@"BookContent_bofang_nl_dk"];
        } else {
            image = [UIImage imageNamed:@"BookContent_bofang_sl_dk"];
        }
    }
    
    [self setImage:image forState:UIControlStateNormal];
    UIView *tempView = nil;
    for (UIView *sub in view.subviews) {
        if ([sub isKindOfClass:[ReaderToolBar class]] || [sub isKindOfClass:[PhoneticReadingToolBar class]]) {
            tempView = sub;
        }
    }
    [view insertSubview:self aboveSubview:view];
    [view insertSubview:self belowSubview:tempView];
}

- (void)hide {
    
    self.hidden = YES;
}
@end
